/*
 * Copyright 2003-2007 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

package com.colony;

import java.io.IOException;
import java.util.HashMap;

import com.argot.TypeException;
import com.argot.TypeLibrary;
import com.argot.remote.MetaLocation;
import com.argot.remote.MetaObject;
import com.colony.remote.RemoteException;

public class ColonyEnvironment
{
	private TypeLibrary _library;
	private IContainer _container;
	
	private HashMap _clients;
	private HashMap _hosts;
	
	public ColonyEnvironment( IContainer container, TypeLibrary library )
	{
		System.out.println("Colony Version 1.2.1");
		System.out.println("Copyright 2004-2007 (C) Live Media Pty Ltd.");
		System.out.println("www.einet.com.au\n");
		
		_library = library;
		_container = container;
		_clients = new HashMap();
		_hosts = new HashMap();
	}
	
	public IContainer getHostContainer()
	{
		return _container;
	}

    public TypeLibrary getTypeLibrary()
    {
    	return _library;
    }
    
	public Object getFront( MetaLocation location ) 
	throws ColonyException, RemoteException, TypeException
	{
		if (location.getHost() == null )
		{
			return _container.getObject(location);
		}

		IColonyClient client = getClient( location.getHost() );
		if ( client == null )
		{
			throw new RemoteException("host connection to host available");
		}
		
		return client.getFront( location );
	}
	
	public Object getFront( MetaObject object )
	throws ColonyException, RemoteException, TypeException
	{
		MetaLocation location = object.getLocation();
		if (location.getHost() == null )
		{
			return _container.getObject(location);
		}

		IColonyClient client = getClient( location.getHost() );
		if ( client == null )
		{
			throw new RemoteException("host connection to host available");
		}
		
		return client.getFront( object );
		
	}
	
	public void addClient( String hostName, IColonyClient client ) 
	throws ColonyException, RemoteException, TypeException, IOException
	{
		client.bind( hostName, this );
		_clients.put( hostName, client );
	}
	
	public IColonyClient getClient( String hostName )
	{
		return (IColonyClient) _clients.get( hostName );
	}
	
	public void addServer( String hostName, IColonyServer server ) 
	throws ColonyException, RemoteException, TypeException
	{
		server.bind( this );
		_hosts.put( hostName, server );
	}

	public void addObject( MetaLocation location, String typeName, Object object )
	throws ColonyException
	{
		_container.addObject(location, typeName, object);
	}
}
